package com.lzy.controller.demo;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lzy.domain.Company;
import com.lzy.domain.Student;
import com.lzy.utils.TimesUtils;

@RestController
public class DemoController {
	
	 @Autowired
	 private MongoTemplate mongoTemplate;
	
	@RequestMapping("/hello")
	public String helloWorld(){
		
		return "hello world";
	}
	


	    /**
	     * 创建对象
	     * @param user
	     */
	
	    @PostMapping("/saveStudent")
	    public String saveUser(@RequestBody Student user) {
	    	 user.setId(UUID.randomUUID().toString().replace("-", ""));
	    	 user.setName(user.getName());
	    	 user.setSex(user.getSex());
	        Student save = mongoTemplate.save(user);
	        
	        return  save.getName();
	    }
	    
	    
	    /**
	     * 创建对象
	     * @param company
	     */
	
	    @PostMapping("/saveCompany")
	    public String saveCompany(@RequestBody Company company) {
	    	company.setId(UUID.randomUUID().toString().replace("-", ""));
	    	company.setName(company.getName());
	    	company.setTime(TimesUtils.getCurrentTime());
	    	company.setNowtime(new Date());
	    	Company com = mongoTemplate.save(company);
	        
	        return  com.getName();
	    }

	     /**
	      * 查询学生  
	      * @return
	      */
	     @GetMapping("/findSudent")
	     public List<Student> findSudent(){
	    	 //Query query = Query.query(Criteria.where("sysName").is(Constant.SYS_TYPE_PORTAL).and("userName").is(account)).limit(5).with(new Sort(Direction.DESC, "create_time "));

	    	 //query=query(criteria).with(newSort(newSort.Order(Sort.Direction.DESC,"updateTime"),new Sort.Order(Sort.Direction.ASC, "titleFull"))).skip((page - 1) * rows).limit(rows);
	    	 
	    	 Query query = new Query();
	    	 query.addCriteria(Criteria.where("name").is("lzy"));
	         List<Student> stus = mongoTemplate.find(query, Student.class, "student");
	         return stus;
	     }
	     
	     /**
	      * 查询公司 
	      * @return
	      */
	     @GetMapping("/findCompany")
	     public List<Company> findCompany(){
	        
	    	 
	    	 /*List<Company> companys = mongoTemplate.findAll(Company.class,"company");
	    	 Sort sort = new Sort(Sort.Direction.ASC, "time").and(new Sort(Sort.Direction.ASC, "time"));//多条件DEVID、time
*/	    	 /*Criteria criteria = Criteria.where(“CHECK”).is(0);//查询条件
	    	 Query query = new Query(criteria);
	    	 mongoTemplate.find(query.with(sort).limit(1000), UnormalInfo.class)；
	    	 */
	    	 Sort sort = new Sort(Sort.Direction.DESC,"nowtime");
	    	 //Criteria criteria = Criteria.where("now").is(null);
	    	 Query query = new Query();
	    	 List<Company> companys = mongoTemplate.find(query.with(sort).limit(5).skip(2),Company.class,"company");
	    	 List<Student> students = mongoTemplate.findAll(Student.class, "student");
			 long totalSize = mongoTemplate.count(query,"company");
	    	 System.out.println("数量"+totalSize);

	    	 for (Student student : students) {
				System.out.println(student.getName());
			}
	    	 
	    	 
	    	 //Sort sort = new Sort(Sort.Direction.DESC,"time");

	    	//完全匹配
	    	/* Pattern pattern1 = Pattern.compile("^张$", Pattern.CASE_INSENSITIVE);
	    	 //右匹配
	    	 Pattern pattern2 = Pattern.compile("^.*张$", Pattern.CASE_INSENSITIVE);
	    	 //左匹配
	    	 Pattern pattern3 = Pattern.compile("^张.*$", Pattern.CASE_INSENSITIVE);
	    	 //模糊匹配
*/	   /* 	 Pattern pattern4 = Pattern.compile("^.*司.*$", Pattern.CASE_INSENSITIVE);
	    	 Criteria criteria = Criteria.where("name").regex(pattern4);
	    	 Query query = new Query(criteria);
	    	 List<Company> companys = mongoTemplate.find(query,Company.class,"company");*/
	    	 
	         return companys;
	     }
	   
	
}
